/**
 * 
 */
package template.page.admin;


import org.apache.click.control.FieldSet;
import org.apache.click.control.Form;
import org.apache.click.control.HiddenField;
import org.apache.click.control.Submit;
import org.apache.click.control.TextField;
import org.apache.click.extras.control.NumberField;
import org.apache.click.util.Bindable;

import cvcg.model.Game;
import template.page.BorderPage;

/**
 * @author Penetrator
 *
 */
@SuppressWarnings("serial")
public class EditGame extends BorderPage {
	
	public String title = "Edit Game";
	private Form gameForm = new Form("gameForm");
	private HiddenField idField = new HiddenField("id",Long.class);
	
	@Bindable protected Long id;
	
	public EditGame() 
	{
		addControl(gameForm);		
		
        FieldSet detailFieldSet = new FieldSet("Details");
        gameForm.add(detailFieldSet);
        gameForm.add(idField);

        TextField nameField = new TextField("name", true);
        nameField.setMinLength(5);
        nameField.setFocus(true);        
        detailFieldSet.add(nameField);
        detailFieldSet.add(new NumberField("minPlayer", true));
        detailFieldSet.add(new NumberField("maxPlayer", true));
        detailFieldSet.add(new NumberField("duration", true));
                      
        gameForm.add(new Submit("ok", " OK ", this, "onOkClicked"));
        gameForm.add(new Submit("cancel", this, "onCancelClicked"));
	}
	
    @Override
    public void onGet() {
        if (id != null) {
            Game game = gameSessionService.getGameById(id);

            if (game != null) {
                // Copy customer data to form. The idField value will be set by
                // this call
            	gameForm.copyFrom(game);
            	title = title + " " + game.getName();
            }
        }
    }

	/**
     * Handle the OK button click event.
     *
     * @return true
     */
    public boolean onOkClicked() {
        if (gameForm.isValid()) 
        {
        	Long idValue = (Long) idField.getValueObject();
        	gameSessionService.saveGame(idValue, gameForm);
        	setRedirect(ManageGames.class);
        	return true;
        }
        
        return false;
    }

    /**
     * Handle the Cancel button click event.
     *
     * @return false
     */
    public boolean onCancelClicked() {
    	setRedirect(ManageGames.class);
        return true;
    }
}
